package com.ming.springboot.mbg;

import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.dom.java.CompilationUnit;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.internal.DefaultCommentGenerator;
import org.mybatis.generator.internal.util.StringUtility;

import java.util.Properties;

/**
 * 自定义注释生成器
 * Created by mqsi on 2022/5/18 15:56
 */
public class CommentGenerator extends DefaultCommentGenerator {
	private boolean addRemarkComments = false;
	private static final String EXAMPLE_SUFFIX="Example";
	private static final String MAPPER_SUFFIX="Mapper";
	private static final String API_MODEL_PROPERTY_FULL_CLASS_NAME = "io.swagger.annotations.ApiModelProperty";

	/**
	 * 设置用户配置的参数
	 */
	@Override
	public void addConfigurationProperties(Properties props) {
		super.addConfigurationProperties(props);
		this.addRemarkComments = StringUtility.isTrue(props.getProperty("addRemarkComments"));
	}

	/**
	 * 给字段添加注释
	 */
	@Override
	public void addFieldComment(Field field, IntrospectedTable introspectedTable, IntrospectedColumn introspectedColumn) {
		String remarks = introspectedColumn.getRemarks();
		//根据参数和备注信息判断是否添加备注信息
		if (addRemarkComments && StringUtility.stringHasValue(remarks)){
			//数据库中特殊字符需要转义
			if (remarks.contains("\"")){
				remarks = remarks.replace("\"", "'");
			}
			//给model的字段添加swagger注解
			field.addJavaDocLine("@ApiModelProperty(value = \""+ remarks + "\")");
		}
	}

	@Override
	public void addJavaFileComment(CompilationUnit compilationUnit) {
		super.addJavaFileComment(compilationUnit);
		//只在model中添加swagger注解类的导入
		String fullyQualifiedName = compilationUnit.getType().getFullyQualifiedName();
		if (!fullyQualifiedName.contains(MAPPER_SUFFIX) && !fullyQualifiedName.contains(EXAMPLE_SUFFIX)){
			compilationUnit.addImportedType(new FullyQualifiedJavaType(API_MODEL_PROPERTY_FULL_CLASS_NAME));
		}
	}
}
